package com.pro.controller;

import com.pro.mapper.JsBannerMapper;
import com.pro.pojo.JsCarousel;
import com.pro.pojo.JsonData;
import com.pro.service.JsBannerService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cyj
 * @date 2022.3.18$ 9.51$
 * @content :
 */
@RestController
@RequestMapping("/newBanner")
public class JsBannerController {

    @Autowired
    private JsBannerService service;

    @Autowired
    private FastDFSClientUtil dfsClient;

    /**
     * @author cyj
     * @date 2022.3.18$ 10.05$
     * @content : 轮播图计数
     */
    @RequestMapping("/getAllBanner")
    public JsonData getAllBanner(String imgType, String display,Integer limit,Integer page){
        List<JsCarousel> jsCarousels = service.getAllBanner(imgType,display,limit,page);
        Integer count = service.countBanners(imgType,display);
        return jsCarousels!=null?JsonData.buildSuccess(jsCarousels,count):JsonData.buildError("暂无轮播图");
    }
    /**
     * @author cyj
     * @date 2022.3.18$ 10.23$
     * @content : 轮播图删除
     */
    @RequestMapping("/delBanner")
    public JsonData getAllBanner(Integer id){
        Boolean flag = service.delBanner(id);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.18$ 10.36$
     * @content : 获取一个轮播图的详细信息
     */
    @RequestMapping("/getOnlyBanner")
    public JsonData getOnlyBanner(Integer id){
        JsCarousel banner = service.getOnlyBanner(id);
        return banner!=null?JsonData.buildSuccess(banner):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.18$ 14.45$
     * @content : 修改一个轮播图的详细信息
     */
    @RequestMapping("/updateBanner")
    public JsonData updateBanner(JsCarousel jsCarousel){
        Boolean flag = service.updateBanner(jsCarousel);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }
    /**
     * @author cyj
     * @date 2022.3.17$ 15.02$
     * @content :上传图片
     */
    @RequestMapping("/uploadFile")
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        System.err.println("开始上传");
        String resAccessUrl =null;
        try {
            String fileUrl = dfsClient.uploadFile(file);
            //路径
            resAccessUrl = dfsClient.getResAccessUrl(fileUrl);

        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        System.err.println(resAccessUrl);
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }
    /**
     * @author cyj
     * @date 2022.3.21$ 13.44$
     * @content : 新增一个轮播图
     */
    @RequestMapping("/addBanner")
    public JsonData addBanner(JsCarousel jsCarousel){
        Boolean flag = service.addBanner(jsCarousel);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务器错误");
    }

}
